<?php
namespace App\Base;

use Illuminate\Support\Facades\DB;

class Base{ 
    /**
     * [baseExport 导出excel基础函数]
     * @Author:  刘家腾
     * @DateTime 2019-04-29
     * @param    arr     $dataList  [数据列表 必传]
     * @param    arr     $Column    [字段列名 必传]
     * @param    string     $fileName  [文件名 必传]
     * @param    arr     $titleList [表格标题  选填]
     * @return   [type]                [直接导出excel表格 固定后缀 .xlsx 格式]
     */
	public static function baseExport($dataList=[],$Column='',$fileName=[],$titleList=[])
	{
        // header("Content-type: text/html; charset=utf-8"); 
		$filename = $fileName;
        $table = '';
        if ( !empty($titleList) ) {
	        foreach ($titleList as $titlek => $titlev) {
	        	$table .= $titlev."\t";
	        }
        }else{
	        foreach ($Column as $ColumnK => $ColumnV) {
	        	$table .= $ColumnV."\t";
	        }
        }
		$table .= "\n";
        foreach ($dataList as $v) {
            foreach ($Column as $ColumnK => $ColumnV) {
	        	$table .= $v[$ColumnV]."\t";
	        }
	        $table .= "\n";
        }
        header("Content-type:application/vnd.ms-excel;charset=UTF-8");
        header("Pragma: public");  
        header("Expires: 0");  
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  
        header("Content-Type:application/force-download");  
        header("Content-Type:application/vnd.ms-execl");  
        header("Content-Type:application/octet-stream");  
        header("Content-Type:application/download");;  
        header('Content-Disposition:attachment;filename='.$fileName.'.xlsx');  
        header("Content-Transfer-Encoding:binary"); 
        echo  $table;
	}
    /**
     * [httpGet curl GET]
     * @Author:  刘家腾
     * @DateTime 2019-05-29
     * @param    [type]     $url    [description]
     * @param    string     $heades [description]
     * @return   [type]             [description]
     */
    public static function httpGet($url,$heades='') {

        $tmpInfo = file_get_contents($url);
        
        return json_decode($tmpInfo, TRUE);    //返回json对象！

        if (empty($url)) {
            return FALSE;
        }
        //初始化
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        if($heades)
        {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $heades);
        }
        $tmpInfo = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);
        //return $tmpInfo;
        return json_decode($tmpInfo, TRUE);    //返回json对象！
    }
    /**
     * [httpPost curl Post]
     * @Author:  刘家腾
     * @DateTime 2019-06-19
     * @param    [type]     $url    [description]
     * @param    string     $params [description]
     * @param    string     $heades [description]
     * @return   [type]             [description]
     */
    public static function httpPost($curl='',$params=''){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $curl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$params );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 6000);
        curl_setopt($ch, CURLOPT_TIMEOUT, 6000);
        $response = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if ($info['http_code'] == 200) {
            return $response;
        } else {
            return null;
        }
    }
    /**
     * [write_log 日志]
     * @Author:  刘家腾
     * @DateTime 2019-05-29
     * @param    [type]     $data     [description]
     * @param    string     $filename [description]
     * @param    boolean    $isarr    [description]
     * @return   [type]               [description]
     */
    public static function write_log($data,$filename='',$isarr=true){ 
        $years = date('Y-m');
        //设置路径目录信息
        
        $url = './logs/'.$years.'/'.$filename.'_'.date('Y-m-d').'_log.txt';  
        
        $dir_name=dirname($url);
        //目录不存在就创建
        if(!file_exists($dir_name))
        {
            //iconv防止中文名乱码
            $res = mkdir(iconv("UTF-8", "GBK", $dir_name),0777,true);
        }
        $fp = fopen($url,"a");//打开文件资源通道 不存在则自动创建   
        if($isarr){
            fwrite($fp,date("Y-m-d H:i:s")."\r\n".var_export($data,true)."\r\n");//写入文件
        }else{
            fwrite($fp,date("Y-m-d H:i:s")."\r\n".$data."\r\n");//写入文件
        }    
        
        fclose($fp);//关闭资源通道
    }
    //根据指定字段排序
    public static function array_sort($arr, $keys, $orderby = 'asc') {
        $keysvalue = $new_array = array();
        foreach ($arr as $k => $v) {
            $keysvalue[$k] = $v[$keys];
        }
        if ($orderby == 'asc') {
            asort($keysvalue);
        } else {
            arsort($keysvalue);
        }
        reset($keysvalue);
        foreach ($keysvalue as $k => $v) {
            $new_array[] = $arr[$k];
        }
        return $new_array;
    }
}